package com.ruoyi.quartz.service.impl;

import com.ruoyi.quartz.domain.Userbalance;
import com.ruoyi.quartz.domain.YyZhouhuibase;
import com.ruoyi.quartz.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class IDataCleanWeeklyServiceImpl implements IDataCleanWeeklyService {
    private static final Logger logger = LoggerFactory.getLogger(IDataCleanWeeklyServiceImpl.class);

    @Autowired
    private IClassAllbudgetBiService classAllbudgetBiService;

    @Autowired
    private IYyZhouhuibaseService zhouhuibaseService;

    @Autowired
    private IUserbalanceService userbalanceService;

    @Override
    public void lyDataClean(){
        List<Map<String,Object>> list = zhouhuibaseService.selectLyUserBalance();
        if(list.size()>0){
            userbalanceService.deleteUserbalance();
            for(Map<String,Object> map:list){
                String uid = map.get("uid").toString();
                List<Map<String,Object>> userInfo = zhouhuibaseService.selectUserInfoByUid(uid);
                Userbalance ub = new Userbalance();
                ub.setUid(uid);
                if(userInfo.size()>0){
                    if(userInfo.get(0)!=null){
                        map.put("name",userInfo.get(0).get("name"));
                        ub.setName(userInfo.get(0).get("name").toString());
                    }
                }
                List<Map<String,Object>> balance = zhouhuibaseService.selectBalanceByUid(uid);
                if(balance.size()>0){
                    if(balance.get(0)!=null){
                        map.put("balance",balance.get(0).get("after_money"));
                    }else{
                        map.put("balance",map.get("recharge"));
                    }
                }else{
                    map.put("balance",map.get("recharge"));
                }
                ub.setBalance(map.get("balance").toString());
                ub.setRecharge(map.get("recharge").toString());
                userbalanceService.insertUserbalance(ub);
            }
        }

    }

    @Override
    public void ysDataClean(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(new Date());
        zhouhuibaseService.deleteYyZhouhuibaseByYear(year);
        List<Map<String,Object>> list = zhouhuibaseService.selectZhouhuiBaseData(year);
        if(list.size()>0){
            for(Map<String,Object> map : list){
                YyZhouhuibase base = new YyZhouhuibase();
                if(map.get("djdate")!=null){
                    base.setDjdate(map.get("djdate").toString());
                }
                if(map.get("predate")!=null){
                    base.setPredate(map.get("predate").toString());
                }
                if(map.get("company")!=null){
                    base.setCompany(map.get("company").toString());
                }
                if(map.get("salename")!=null){
                    base.setSalename(map.get("salename").toString());
                }
                if(map.get("dpt")!=null){
                    base.setDpt(map.get("dpt").toString());
                }
                if(map.get("bigdpt")!=null){
                    base.setBigdpt(map.get("bigdpt").toString());
                }
                if(map.get("bigname")!=null){
                    base.setBigname(map.get("bigname").toString());
                }
                if(map.get("xlname")!=null){
                    base.setXlname(map.get("xlname").toString());
                }
                if(map.get("brand")!=null){
                    base.setBrand(map.get("brand").toString());
                }
                if(map.get("money")!=null){
                    base.setMoney(map.get("money").toString());
                }
                if(map.get("qty")!=null){
                    base.setQty(map.get("qty").toString());
                }
                if(map.get("cls")!=null){
                    base.setCls(map.get("cls").toString());
                }
                if(map.get("wk_year")!=null){
                    base.setWkYear(map.get("wk_year").toString());
                }
                if(map.get("ym")!=null){
                    base.setYm(map.get("ym").toString());
                }
                zhouhuibaseService.insertYyZhouhuibase(base);
            }
        }


    }
}
